
function injectCustomJs (jsPath) {
  jsPath = jsPath || './js/injectJs.js'
  var temp = document.createElement('script')
  temp.setAttribute('type', 'text/javascript')
  // 获得的地址类似：chrome-extension://ihcokhadfjfchaeagdoclpnjdiokfakg/js/inject.js
  temp.src = chrome.extension.getURL(jsPath)
  temp.onload = function () {
    // 放在页面不好看，执行完后移除掉
    this.parentNode.removeChild(this)
  }
  document.head.appendChild(temp)
}
// window.addEventListener('message', function (message) {
//   console.log('message', message)
//   if (message.data.action == 'londed') {
//     layer = JSON.parse(message.data.layer)
//   }
// }, false)
injectCustomJs()
function showMsg (msg) {
  window.postMessage({ 'action': 'layer', 'msg': '插件提示：' + msg }, '*')
}
setTimeout(function () {
  var isdo = false
  var isLogin = true
  var isloaded = false
  var nativeLogin = false
  var canFlash = false
  var isReSub = false
  var subMin = 0.002
  var yue = 0
  var isTrySub = false
  $(function () {
    var isCanuser = false
    var $this = $('.dataFormInvest')
    isCanuser = $('.dataFormInvest').length
    function inituser () {
      var formKey
      var youhave = $('.p_zc01').parent().find('h4').html() - 0
      if (yue > youhave) {
        chrome.runtime.sendMessage({
          action: 'beginSub',
          isStop: true,
          type: 2
        },
        function (response) {
          showMsg('本次已经提交成功了')
        })
      }
      // 测试
      // var test = true
      // if (test) {
      //   var investNumber = 0
      //   // 单次 最多
      //   var maxNumber = 0.3
      //   youhave = 0.16
      //   // var isTrySubjectFlag = true
      //   if (yue > youhave && !isTrySub) {
      //     showMsg('本次已经提交成功了，已经停止再次提交')
      //     console.log('本次已经提交成功了，已经停止再次提交,提交前：' + yue + '，提交后:' + youhave)
      //     return false
      //   }
      //   if (yue > 0.000000001) {
      //     investNumber = Math.min(yue, youhave)
      //   } else {
      //     investNumber = youhave
      //   }
      //   //
      //   investNumber = Math.min(maxNumber, investNumber)
      //   var minNumber = 0.002
      //   // investNumber = Math.max(minNumber, investNumber)
      //   if (!minNumber) {
      //     minNumber = 0
      //   }
      //   console.log('youhave:' + youhave + '币;提交：' + investNumber)
      //   if (youhave >= subMin && investNumber - 0 >= minNumber) {
      //     showMsg('您的余额：' + youhave + '币;余额不足' + subMin + '或者' + minNumber)
      //     console.log('您的余额：' + youhave + '币;余额不足' + subMin + '或者' + minNumber + '；提交:' + investNumber)
      //   }
      // }
      if (isCanuser) {
        $('body,html').animate({
          scrollTop: $this.offset().top // 让body的scrollTop等于pos的top，就实现了滚动
        }, 0)
        formKey = parseInt($this.data('id'))
        var investNumber = 0
        // 单次 最多
        var maxNumber = $('#max_number' + formKey).val()
        // var isTrySubjectFlag = true
        if (yue > youhave && !isTrySub) {
          showMsg('本次已经提交成功了，已经停止再次提交')
          console.log('本次已经提交成功了，已经停止再次提交,提交前：' + yue + '，提交后:' + youhave)
          return false
        }
        if (yue > 0.000000001) {
          investNumber = Math.min(yue, youhave)
        } else {
          investNumber = youhave
        }
        //
        investNumber = Math.min(maxNumber, investNumber)
        // 好像是盘总数剩余
        // var surplus_number = $('#surplus_number' + formKey).val()
        // investNumber = Math.min(surplus_number, investNumber)
        // 必须大于最小
        var minNumber = $('#min_number' + formKey).val() || 0
        // investNumber = Math.max(minNumber, investNumber)
        if (!minNumber) {
          minNumber = 0
        }
        console.log('youhave:' + youhave + '币;提交：' + investNumber)
        if (youhave >= subMin && investNumber - 0 >= minNumber) {
          $('#number' + formKey).val(investNumber)
          chrome.runtime.sendMessage({
            action: 'beginSub',
            isStop: false,
            type: 1
          },
          function (response) {
            if (response.state == 'beginSub:ok') {
              setTimeout(function () {
                $this.trigger('submit')
                setTimeout(function () {
                  $this.trigger('submit')
                  // if (isReSub) {
                  doReflash(100)
                  // }
                }, 100)
              }, 100)
            }
          })
        } else {
          $this.parent().append("<div style='color:red;'>您的余额：" + investNumber + "'币;余额不足'" + subMin + '或者' + minNumber + '</div>')
          showMsg('您的余额：' + youhave + '币;余额不足' + subMin + '或者' + minNumber)
          console.log('您的余额：' + youhave + '币;余额不足' + subMin + '或者' + minNumber)
        }
      }
    }
    function initNativeLogin (type, addr) {
      $('.login_but ul li').eq(type - 1).trigger('click')
      $('#address').val(addr)
      setTimeout(function () {
        $('.binding_but').trigger('click')
      }, 100)
    }
    isRunning()
    function addLis () {
      chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
          if (request.action == 'begin' && !isdo && canFlash) {
            if (!isCanuser) {
              doReflash(request.ping)
            }
            sendResponse({ state: 'begin:ok', isStop: false })
            isdo = true
          }
          if (request.action == 'nativeLogin' && !isdo) {
            initNativeLogin(request.type, request.addr)
            sendResponse({ state: 'nativeLogin:ok', isStop: false })
            isdo = true
          }
          if (request.action == 'getYue') {
            yue = $('.p_zc01').parent().find('h4').html() - 0 || 0.000000001
            var touzi = $('.p_zc02').parent().find('h4').html()
            sendResponse({state: 'getYue:ok', yue: yue, touzi: touzi})
          }
        }
      )
    }
    function isRunning () {
      chrome.runtime.sendMessage({
        action: 'onLoaded',
        isStop: false,
        location: window.location
      }, function (response) {
        if (response.state == 'onLoaded:ok') {
          console.log('onLoaded:ok', response, 'hanve:', response.yue, $('.p_zc01').parent().find('h4').html() - 0)
          isLogin = response.isLogin
          nativeLogin = response.nativeLogin
          canFlash = response.canFlash
          subMin = response.subMin
          yue = response.yue
          isTrySub = response.isTrySub
          isloaded = true
          if (response.isLogin) {
            inituser()
            addLis()
            console.log('插件正在工作。。。')
          } else {
            console.log('插件需要登录后才能工作')
          }
          if (!nativeLogin && response.addr) {
            setTimeout(function () {
              initNativeLogin(response.type, response.addr)
            }, 100)
          }
        }
      })
    }
    function doReflash (time) {
      console.log('window.location.reload')
      setTimeout(function () {
        window.location.reload()
      }, time)
    }
  })
}, 200)
